package com.example.administrator.szb.fragments.fragment_forWD;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.JSWebViewActivity;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.bean.Business;
import com.example.administrator.szb.fragments.base.MVPBaseFragment;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by stone on 2018/1/2.
 */

public class FragmentAskContent  extends MVPBaseFragment {
    private View tops_tips_close;
    private TextView title_jianjie;
    private int type = 1;//1热门2最新3待回答(必填)
    private FragmentManager fm;
    private Business business;
    private LinearLayout tops_tips;
    private int business_id = 0;//全部业务0具体业务是具体业务的id
    private List<FragmentAskContent2> fragmentAskContentList = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragmentaskcontentf,null);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
    TextView tab_1,tab_2,tab_3;
    @Override
    protected void initView(View view) {

        fm = getChildFragmentManager();
        tops_tips_close = view.findViewById(R.id.tops_tips_close);
        tops_tips = view.findViewById(R.id.tops_tips);
        title_jianjie = view.findViewById(R.id.title_jianjie);
          tab_1  = view.findViewById(R.id.tab_1);
          tab_2  = view.findViewById(R.id.tab_2);
         tab_3  = view.findViewById(R.id.tab_3);
        tab_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showFragment(0);
            }
        });
        tab_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showFragment(1);
            }
        });
        tab_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showFragment(2);
            }
        });

    }
    public FragmentTransaction getFragmentTransaction(){

        FragmentTransaction transaction = fm.beginTransaction();

        return transaction;
    }
    @Override
    protected void initEvent() {

        tops_tips_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tops_tips.setVisibility(View.GONE);
            }
        });
        fragmentAskContentList.add(null);
        fragmentAskContentList.add(null);
        fragmentAskContentList.add(null);

        title_jianjie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //查看简介
                Intent intent = new Intent(getActivity(),JSWebViewActivity.class);
                intent.putExtra("title",business.getTitle());
                intent.putExtra("id",business_id);
                startActivity(intent);

            }
        });

    }
    private void showFragment(int i){
        type=i+1;
        tab_1.setTextColor(Color.parseColor("#c9c9c9"));
        tab_2.setTextColor(Color.parseColor("#c9c9c9"));
        if (i==0){
            tab_1.setTextColor(Color.parseColor("#333333"));
        }else if (i==1){
            tab_2.setTextColor(Color.parseColor("#333333"));
        }
        FragmentTransaction transaction = getFragmentTransaction();
        hideFragment(transaction);
        if (fragmentAskContentList.get(i)==null){
            FragmentAskContent2 fragmentAskContent = new FragmentAskContent2();
            fragmentAskContent.setDatas(business,type);
            fragmentAskContentList.set(i,fragmentAskContent);


            transaction.add(R.id.frameLayout,fragmentAskContentList.get(i));
        }else {
            transaction.show(fragmentAskContentList.get(i));
        }
        transaction.commitAllowingStateLoss();
    }

    private void hideFragment(FragmentTransaction transaction){
        for (int i=0;i<fragmentAskContentList.size();i++){
            if (fragmentAskContentList.get(i)!=null){
                transaction.hide(fragmentAskContentList.get(i));
            }
        }
    }

    @Override
    protected void initData() {
        if (business!=null){
            if (business.getId()!=0){
                title_jianjie.setText(business.getTitle()+"的简介");
            }else {
                tops_tips.setVisibility(View.GONE);
            }
        }
        showFragment(0);

    }

    /**
     * 从FragmentAsk传来的值
     * @param b
     */
    public void setDatas(Business b){
        this.business = b;
        business_id = this.business.getId();
    }

}
